package com.pixiv.service.Impl;

import com.pixiv.DAO.LoginMapping;
import com.pixiv.Entity.User;
import com.pixiv.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private LoginMapping loginMapping;

    @Override
    public User getUserByLoginParameter(String userName, String pwd) {
        if (pwd != null)
        {
            String pwdMd5 = DigestUtils.md5DigestAsHex(pwd.getBytes());
            User u = loginMapping.getUserByLoginParameter(userName,pwdMd5);
            return u;
        }else
        {
            User u = loginMapping.getUserByUserName(userName);
            return u;
        }
    }

    public Date getLastLoginTimeByUserID(int id)
    {
        Date date = loginMapping.getLastLoginTimeByUserID(id);
        return date;
    }

    @Override
    public User initLoginUserData(User u) {
        //int id = loginMapping.getUserIDByUserName(u.getUserName() );

        //u.setUserID(id);
        //u.setName( loginMapping.getNameByID(id) );
        //u.setEMail( loginMapping.getEMailByID(id) );
        //u.setPhone( loginMapping.getPhoneByID(id ));
        //u.setQQ( loginMapping.getQQByID(id) );
        //u.setSex( loginMapping.getSexByID(id));
        //u.setLevel( loginMapping.getUserLevelByUserID(id) );
        //u.setExplosion( loginMapping.getUserExplosionByUserID(id) );
        //u.setUserType( loginMapping.getUserTypeByUserID(id) );
        return u;
    }

    @Override
    public void flushUserLoginTime(int userid) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String date = sdf.format(new Date());
        loginMapping.flushUserLoginTimeByID(userid,date);
    }


    public void insertInitUserLevelInfor(int id)
    {
        //System.out.println(UL.getUserLevelExplosionSizeByUserID(id) );
        if (loginMapping.getUserLevelExplosionSizeByUserID(id) <= 0) {
            loginMapping.insertInitUserLevelExplosionByUserID(id);
        }
    }

    public void insertInitUserIconByPath(int id)
    {
        if (loginMapping.getUserIconSizeIDByUserID(id) <= 0) {
            loginMapping.insertUserIconPathByUserID(id, "");
        }
    }


    public void updateUserType(int type,int id)
    {
        loginMapping.updateUserTypeByUserID(type,id);
    }


}
